package com.renjibo.sharkerwave.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.renjibo.sharkerwave.R;
import com.renjibo.sharkerwave.models.Course;

import java.util.List;

/**
 * 第三种布局的适配
 * 任继波
 * Created by Administrator on 2017/4/10.
 */

public class HomeThreeAdapter extends RecyclerView.Adapter<HomeThreeAdapter.OneViewHolder> {

    private final LayoutInflater inflater;
    private List<Course> topicCourse;
    private Context context;
    private RecyclerViewItemOnClick recyclerViewItemOnClick;
    public HomeThreeAdapter(Context context, List<Course> topicCourse) {
        inflater = LayoutInflater.from(context);
        this.topicCourse = topicCourse;
        this.context = context;
    }
    public void setRecyclerViewItemOnClick(RecyclerViewItemOnClick recyclerViewItemOnClick){
        this.recyclerViewItemOnClick=recyclerViewItemOnClick;
    }
    @Override
    public OneViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.home_three_recycler_item, parent, false);
        return new OneViewHolder(view);
    }

    @Override
    public void onBindViewHolder(OneViewHolder holder, final int position) {
        final Course course = topicCourse.get(position);
        holder.home_topic_text_title.setText(course.title);
        holder.home_topic_text_title2.setText(course.title2);
        if (course.type == 1) {
            Glide.with(context).load(R.drawable.home_image_small_video).into(holder.home_topic_recycler_image_small);
        } else {
            Glide.with(context).load(R.drawable.home_image_small_music).into(holder.home_topic_recycler_image_small);
        }
        Glide
                .with(context)
                .load(course.image)
                .centerCrop()
                .placeholder(R.drawable.home_details_head)
                .crossFade()
                .error(R.drawable.home_details_head)
                .into(holder.home_topic_recycler_image_big);
        holder.home_topic_recuycler_auto_linear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(recyclerViewItemOnClick!=null)
                    recyclerViewItemOnClick.onItemClick(position,course.object_id);
            }
        });
    }

    @Override
    public int getItemCount() {
        return topicCourse.size();
    }

    class OneViewHolder extends RecyclerView.ViewHolder {

        private final TextView home_topic_text_title2;
        private final TextView home_topic_text_title;
        private final ImageView home_topic_recycler_image_small;
        private final ImageView home_topic_recycler_image_big;
        private final View home_topic_recuycler_auto_linear;

        public OneViewHolder(View itemView) {
            super(itemView);
            home_topic_recuycler_auto_linear =itemView.findViewById(R.id.home_topic_recuycler_auto_linear);
            home_topic_text_title2 = (TextView) itemView.findViewById(R.id.home_topic_text_title2);
            home_topic_text_title = (TextView) itemView.findViewById(R.id.home_topic_text_title);
            home_topic_recycler_image_small = (ImageView) itemView.findViewById(R.id.home_topic_recycler_image_small);
            home_topic_recycler_image_big = (ImageView) itemView.findViewById(R.id.home_topic_recycler_image_big);
        }
    }
}
